package com.ruoyi.neo.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.neo.domain.LinkPandCDTO;
import com.ruoyi.neo.domain.PartCategory;
import com.ruoyi.neo.service.IPartCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.neo.domain.Part;
import com.ruoyi.neo.service.IPartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 零件信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-12
 */
@RestController
@RequestMapping("/neo/part")
@Slf4j
public class PartController extends BaseController
{
    @Autowired
    private IPartService partService;
    /**
     * 查询零件信息列表
     */
//    @PreAuthorize("@ss.hasPermi('neo:part:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(Part part)
//    {
//        startPage();
//        List<Part> list = partService.selectPartList(part);
//        return getDataTable(list);
//    }

    @GetMapping("/list")
    public AjaxResult getPartCategoryGraph() {
        List<Part> parts = partService.selectPartList(new Part());
        return success(parts);
    }

    /**
     * 导出零件信息列表
     */
    @PreAuthorize("@ss.hasPermi('neo:part:export')")
    @Log(title = "零件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Part part)
    {
        List<Part> list = partService.selectPartList(part);
        ExcelUtil<Part> util = new ExcelUtil<Part>(Part.class);
        util.exportExcel(response, list, "零件信息数据");
    }

    /**
     * 获取零件信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('neo:part:query')")
    @GetMapping(value = "/{partNumber}")
    public AjaxResult getInfo(@PathVariable("partNumber") String partNumber)
    {
        return success(partService.selectPartByPartNumber(partNumber));
    }

    /**
     * 新增零件信息
     */
    @PreAuthorize("@ss.hasPermi('neo:part:add')")
    @Log(title = "零件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Part part)
    {
        return toAjax(partService.insertPart(part));
    }

    /**
     * 修改零件信息
     */
    @PreAuthorize("@ss.hasPermi('neo:part:edit')")
    @Log(title = "零件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Part part)
    {

        return toAjax(partService.updatePartInNeo4j(part));
    }

    /**
     * 删除零件信息
     */
    @PreAuthorize("@ss.hasPermi('neo:part:remove')")
    @Log(title = "零件信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{partNames}")
    public AjaxResult remove(@PathVariable String[] partNames)
    {

        return toAjax(partService.deletPartByNamesInNeo4j(partNames));
    }

    //添加零件与零件分类的关联
    @PreAuthorize("@ss.hasPermi('neo:part:addLink')")
    @PostMapping("/addLink")
    public AjaxResult addLinkBetweenPartAndCategory(@RequestBody LinkPandCDTO linkPandCDTO){
        return toAjax( partService.linkPartAndCategory(linkPandCDTO.getPartName(),linkPandCDTO.getCategoryName()));
    }


}
